﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCM.UI
{
    /// <summary>  
    /// Title:ZMCMJ2  采购计划查询
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: wangkun
    /// Version: 1.0
    /// History:
    ///       2011-12-13  wangkun   [创建]
    ///<summary>  
    public partial class ZMCMJ2 : EFViewCmes
    {

        public ZMCMJ2()
        {
            InitializeComponent();
            InitControls();
            this.ddMaterialType.DropDownClosed += ddMaterialType_DropDownClosed;
        }

        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            String fdMaterialId = "";
            if (null != this.grid.SelectedItem)
            {
                fdMaterialId = (String)this.grid.SelectedItem.GetType().GetProperty("fdMaterialId").GetValue(this.grid.SelectedItem, null);
                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdMaterialId", fdMaterialId.ToString());
                //this.container.LoadView("ZMCMJ6", args);
            }
            else
            {
                this.container.Content = null;
            }
        }

        public static void AddEIColumnsToEIBlock(EIBlock block, string[] columns)
        {
            EIColumn c;
            foreach (var item in columns)
            {
                c = new EIColumn();
                c.SetName(item);
                block.AddColumn(c);
            }
        }

        private void InitControls()
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCMJ1");
            eiInfo.Set("method", "queryMaterialType");

            EIBlock conBlock = eiInfo.AddBlock("inqu_status");
            AddEIColumnsToEIBlock(conBlock, new string[] { "node" });
            EIRow conRow = new EIRow();
            conRow.Add("node", "root");
            conBlock.AddRow(conRow);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, queryMaterialTypeCompleted);
        }

        private void queryMaterialTypeCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eiInfo = e.EIInfo;
            EIBlock resultBlock = eiInfo.GetBlock("result");
            ddMaterialType.BindItemsToGrid(resultBlock.Rows, "fdTypeName");
        }

        private void ddMaterialType_DropDownClosed(object sender, object e)
        {
            IList<CheckBox> selectedMaterialType = ddMaterialType.LstCheckedItems;
            string materialType = "''";
            foreach (var item in selectedMaterialType)
            {
                materialType = materialType + ",'" + (item.DataContext as EIRow)["fdTypeCode"].ToString() + "'";
            }

            if ("''".Equals(materialType))
            {
                this.fdTypeCode.Value = "";
            }
            else
            {
                this.fdTypeCode.Value = materialType;
            }
        }
    }
}
